package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.commo.utils.R;
import com.qf.entity.User;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserService userService;
    //添加用户
    @PostMapping("/saveUser")
    public R save(@RequestBody User user){

        System.out.println(user);
        boolean save =userService.save(user);
        return R.out(save);
    }
    //分页
    @PostMapping("/page")
    public R page(@RequestBody Page<User> page){
        page =userService.page(page);
        return R.putDate("page",page);
    }
    //通过id查询用户
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Integer id){
        User user = userService.getById(id);
        System.out.println(user);
        return R.putDate("data",user);
    }
    //删除用户
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        boolean b = userService.removeById(id);
        return R.out(b);
    }
    @PostMapping("/update")
    public R update(@RequestBody User user){
        return R.out(userService.updateById(user));
    }

    @PostMapping("/list")
    public R getAllUser(@RequestBody Map<String,Object> map){
        String userName = map.get("username").toString();
        if (!StringUtils.isEmpty(userName)){//如果这个值为空返回为true,取反为false
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.like("username",map.get("username"));
            return R.putDate("data",userService.list(queryWrapper));
        }

        return R.putDate("data",userService.list());
    }
}
